package com.moorecat.app;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;

import com.moorecat.MooerApplication;
import com.moorecat.R;
import com.moorecat.utils.ui.UiUtils;

public class Sign extends TabActivity implements OnClickListener {

	private TabHost tabHost;

	private TabWidget tabWidget;

	private ImageView signNear, signMe, signBusiness;

	private List<View> vs;

	private OnPreDrawListener op;

	private String tab = "business";

	private MooerApplication mooerApp;

	private boolean isWaitForLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.sign);
		mooerApp = (MooerApplication) getApplication();
		Intent intent = getIntent();
		if (intent.getStringExtra("tab") != null) {
			tab = intent.getStringExtra("tab");
		}
		vs = new ArrayList<View>();
		signNear = (ImageView) findViewById(R.id.sign_nearby);
		signMe = (ImageView) findViewById(R.id.sign_me);
		signBusiness = (ImageView) findViewById(R.id.sign_business);
		vs.add(signBusiness);
		vs.add(signMe);
		vs.add(signNear);
		op = UiUtils.initButtonState(signBusiness);
		signNear.setOnClickListener(this);
		signMe.setOnClickListener(this);
		signBusiness.setOnClickListener(this);
		initTab();
	}

	private void initTab() {
		tabHost = getTabHost();
		Intent layout1intent = new Intent();
		layout1intent.setClass(this, SignNear.class);
		final TabHost.TabSpec layout1spec = tabHost.newTabSpec("near");
		layout1spec.setIndicator("");
		layout1spec.setContent(layout1intent);
		tabHost.addTab(layout1spec);

		Intent layout2intent = new Intent();
		layout2intent.setClass(this, SignMe.class);
		final TabHost.TabSpec layout2spec = tabHost.newTabSpec("me");
		layout2spec.setIndicator("");
		layout2spec.setContent(layout2intent);
		tabHost.addTab(layout2spec);

		Intent layout3intent = new Intent();
		layout3intent.setClass(this, SignBusiness.class);
		final TabHost.TabSpec layout3spec = tabHost.newTabSpec("business");
		layout3spec.setIndicator("");
		layout3spec.setContent(layout3intent);
		tabHost.addTab(layout3spec);

		tabWidget = tabHost.getTabWidget();
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			View view = tabWidget.getChildAt(i);
			view.setBackgroundColor(0);
		}
		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				if ("business".equals(tabId)) {
					UiUtils.buttonStateChange(vs, signBusiness, op);
				} else if ("me".equals(tabId)) {

					UiUtils.buttonStateChange(vs, signMe, op);
				} else if ("near".equals(tabId)) {
					UiUtils.buttonStateChange(vs, signNear, op);
				}

			}
		});
		tabHost.setCurrentTabByTag(tab);

	}

	@Override
	public void onClick(View v) {
		if (v == signBusiness) {
			UiUtils.buttonStateChange(vs, signBusiness, op);
			tabHost.setCurrentTabByTag("business");
		} else if (v == signMe) {
			if (mooerApp.isLoggedin()) {
				UiUtils.buttonStateChange(vs, signMe, op);
				tabHost.setCurrentTabByTag("me");
			} else {
				Intent intent = new Intent(this, MoreAccountSetting.class);
				intent.putExtra("isFinish", true);
				startActivity(intent);
				isWaitForLogin = true;
			}
		} else if (v == signNear) {
			UiUtils.buttonStateChange(vs, signNear, op);
			tabHost.setCurrentTabByTag("near");
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (isWaitForLogin && mooerApp.isLoggedin()) {
			UiUtils.buttonStateChange(vs, signMe, op);
			tabHost.setCurrentTabByTag("me");
		}
        //判断是否获取到经纬度，如果不能则提示
        if (mooerApp.getLatitude() == 0.0 && mooerApp.getLongitude() == 0.0) {
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                AlertDialog.Builder ad = new AlertDialog.Builder(this);
                ad.setTitle(R.string.warn);
                ad.setMessage(R.string.gps_warn);
                ad.setPositiveButton(R.string.setting_gps, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent intent = new Intent();
                        intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivity(intent);
                    }
                });
                ad.setNeutralButton(R.string.back, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                           Sign.this.finish();
                    }
                });
                ad.show();
            }

        }
	}

}
